package com.comparator;
/**
 *
 **/

import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * @Author weiwei
 * @Date 2022-09-03 15:38
 * @description
 **/
public class Comparator {

    // list.sort() 如果比较字段为空

    @Data
    static class User {

        private String currentFlag;

        private String takeFlag;
    }

    public static void main(String[] args) {
        List<User> result = new ArrayList<>();
        User userSetMealResp = new User();
        userSetMealResp.setCurrentFlag("1");
        User userSetMealResp1 = new User();
        User userSetMealResp2 = new User();
        userSetMealResp2.setTakeFlag("1");

        result.add(userSetMealResp1);
        result.add(userSetMealResp2);
        result.add(userSetMealResp);
        result.sort(java.util.Comparator.nullsLast(java.util.Comparator.comparing(mapOrDefault(User::getCurrentFlag)).thenComparing(mapOrDefault(User::getTakeFlag))));
        System.out.println(result);

        System.out.println(Optional.empty().toString());
    }

    private static Function<User,String> mapOrDefault(Function<User, String> func){
        //用Optional来设置默认值，如果为空就设置为你想要的规则，比如我这里，想放最后
        //这里比较的因为都是英语单词，Z就是最大的，ASCII码是90，比Z大1的就是91
        return vo -> Optional.ofNullable(func.apply(vo)).orElse("91");
    }
}
